package com.macrounion.atv.utils;

import cn.hutool.core.text.csv.*;
import com.macrounion.atv.config.FileConfig;
import com.macrounion.atv.service.dto.ProtocolONIDto;
import com.macrounion.atv.service.entity.Protocol;
import com.macrounion.atv.service.entity.ProtocolC;
import com.macrounion.atv.service.utils.FileHandleUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class SimpleCsvUtils {

    private static final String staticPath = "static/upload/csv/";
    private static final String protocolHeadPre = "protocol_";
    private static final String protocolcHeadPre = "protocolc_";

    /**
     * 协议转出为csv文件
     *
     * @param out
     * @return csv文件链接
     */
    public static String protocol2Csv(String uploadPath, ProtocolONIDto out) throws IOException {
        System.out.println(uploadPath);
        //初始化变量
        Protocol protocol = out.getProtocol();
        List<ProtocolC> protocolCList = out.getProtocolCList();
        List<String> protocolHeads = new ArrayList<>();
        List<String> protocolcHeads = new ArrayList<>();
        List<String> protocolValues = new ArrayList<>();
        List<List<String>> protocolcValuesList = new ArrayList<>();
//        String absolutePath = FileHandleUtil.createDirIfNotExists();
//        File csvFile = new File(absolutePath, "static/upload/csv/" + protocol.getName() + ".csv");
        File csvFile = new File(uploadPath + "/" + protocol.getName() + ".csv");
        System.out.println(csvFile.getAbsolutePath());
        CsvWriter writer = CsvUtil.getWriter(csvFile, StandardCharsets.UTF_8);
        //封装每行的值
        protocolHeads.add(protocolHeadPre + "name");
        protocolValues.add(protocol.getName());
        protocolHeads.add(protocolHeadPre + "stype");
        protocolValues.add(protocol.getStype() == null ? "" : protocol.getStype());
        protocolHeads.add(protocolHeadPre + "remark");
        protocolValues.add(protocol.getRemark() == null ? "" : protocol.getRemark());
        protocolHeads.add(protocolHeadPre + "snmpv");
        protocolValues.add(protocol.getSnmpv() == null ? "" : protocol.getSnmpv() + "");
        protocolcHeads.add(protocolcHeadPre + "name");
        protocolcHeads.add(protocolcHeadPre + "mid");
        protocolcHeads.add(protocolcHeadPre + "trap");
        protocolcHeads.add(protocolcHeadPre + "channelCode");
        protocolcHeads.add(protocolcHeadPre + "dataType");
        protocolcHeads.add(protocolcHeadPre + "unit");
        for (ProtocolC protocolC : protocolCList) {
            List<String> protocolcValues = new ArrayList<>();
            protocolcValues.add(protocolC.getName());
            protocolcValues.add(protocolC.getMid());
            protocolcValues.add(protocolC.getTrap() + "");
            protocolcValues.add(protocolC.getChannelCode());
            protocolcValues.add(protocolC.getDataType() == null ? "" : protocolC.getDataType() + "");
            protocolcValues.add(protocolC.getUnit() == null ? "" : protocolC.getUnit());
            protocolcValuesList.add(protocolcValues);
        }
        //写入每行数据
        StringBuilder sb = new StringBuilder();
        for (String protocolHead : protocolHeads) {
            sb.append(protocolHead).append(",");
        }
        writer.write(protocolHeads.toArray(new String[0]));
        sb.append("\n");
        for (String protocolValue : protocolValues) {
            sb.append(protocolValue).append(",");
        }
        writer.write(protocolValues.toArray(new String[0]));
        sb.append("\n");
        for (String protocolcHead : protocolcHeads) {
            sb.append(protocolcHead).append(",");
        }
        writer.write(protocolcHeads.toArray(new String[0]));
        sb.append("\n");
        for (List<String> list : protocolcValuesList) {
            for (String protocolcValue : list) {
                sb.append(protocolcValue).append(",");
            }
            writer.write(list.toArray(new String[0]));
            sb.append("\n");
        }
        writer.flush();
        writer.close();
//        return sb.toString();
//        return "/upload/csv/" + protocol.getName() + ".csv";
        return uploadPath + "/" + protocol.getName() + ".csv";
    }

    public static ProtocolONIDto csv2Protocol(MultipartFile multipartFile) throws IOException {
        ProtocolONIDto protocolONIDto = new ProtocolONIDto();
        Protocol protocol = new Protocol();
        List<ProtocolC> protocolCS = new ArrayList<>();
        String filePath = FileHandleUtil.upload(multipartFile.getInputStream(), "csv/", multipartFile.getOriginalFilename());
        CsvReader csvReader = CsvUtil.getReader();
        String absolutePath = FileHandleUtil.createDirIfNotExists();
        File csvFile = new File(absolutePath, "static" + filePath);
        System.out.println(csvFile.getAbsoluteFile());
        CsvData csvData = csvReader.read(csvFile);
        List<CsvRow> rows = csvData.getRows();
        for (int i = 0; i < rows.size(); i++) {
            if (i == 1) {
                CsvRow protocolValueRow = csvData.getRow(i);
                protocol.setName(protocolValueRow.get(0));
                protocol.setStype(protocolValueRow.get(1));
                protocol.setRemark(null == protocolValueRow.get(2) || protocolValueRow.get(2).equals("") ? null : protocolValueRow.get(2));
                protocol.setSnmpv(null == protocolValueRow.get(3) || protocolValueRow.get(3).equals("") ? null : Integer.parseInt(protocolValueRow.get(3)));
            } else if (i >= 3) {
                ProtocolC protocolC = new ProtocolC();
                CsvRow protocolcValueRow = csvData.getRow(i);
                protocolC.setName(protocolcValueRow.get(0));
                protocolC.setMid(protocolcValueRow.get(1));
                protocolC.setTrap(Integer.parseInt(protocolcValueRow.get(2)));
                protocolC.setChannelCode(protocolcValueRow.get(3));
                protocolC.setDataType(null == protocolcValueRow.get(4) || protocolcValueRow.get(4).equals("") ? null : Integer.parseInt(protocolcValueRow.get(4)));
                protocolC.setUnit(null == protocolcValueRow.get(5) || protocolcValueRow.get(5).equals("") ? null : protocolcValueRow.get(5));
                protocolCS.add(protocolC);
            }
        }
        protocolONIDto.setProtocol(protocol);
        protocolONIDto.setProtocolCList(protocolCS);
        return protocolONIDto;
    }

}
